<template>
    <transition name="modal">
        <div class="modal-mask">
            <div class="modal-wrapper">
                <div class="modal-container">
    
                    <div class="modal-header">
                        <div class="row">
                            <div class="col-lg-3">
                                <h3>{{title_str}}</h3>
                            </div>
                            <div class="col-lg-3">
                                <div class="form-horizontal">
                                    <div class="form-group">
                                        <label class="col-lg-6 control-label">过滤检测项：</label>

                                        <div class="col-lg-6">
                                            <input type="text" class="form-control" v-model="auto_str">
                                        </div>
                                    </div>
                                </div>
                                
                            </div>
                            <div class="col-lg-6 text-right">
                                <span><b>md5：</b><a :download="sample.sample_hash" :href="sample.download_url">{{sample.sample_hash}}</a></span>&nbsp;&nbsp;&nbsp;
                                <span><b>程序名：</b>{{sample.program_name}}</span>&nbsp;&nbsp;&nbsp;
                                <button class="btn" :class="{'btn-danger':doubt=='1','btn-default':doubt=='0'}" @click.prevent="setDoubt">有疑问的</button>&nbsp;&nbsp;&nbsp;
                            </div>
                        </div>
                        <button type="button" class="close" @click="close">
                            <i class="fa fa-times" aria-hidden="true"></i>
                        </button>
                    </div>
    
                    <div class="modal-body">
                        <form id="fdForm" enctype="multipart/form-data">
                        <div class="row m-b">
                            <div class="col-lg-1"><b>序号</b></div>
                            <div class="col-lg-6"><b>检测项</b></div>
                            <div class="col-lg-1"><b>检测结果</b></div>
                            <div class="col-lg-2"><b>截图</b></div>
                            <div class="col-lg-2"><b>备注</b></div>
                        </div>
                        <div class="list-box">
                            <input name="sample_hash" type="hidden" :value="sample.sample_hash">
                            <input name="doubt" type="hidden" :value="doubt">
                            <template v-for="(analysis,i) in res_list">
                                <template v-if="analysis.name.indexOf(auto_str)!=-1||(i+1).toString().indexOf(auto_str)!=-1">
                                    <div class="row" :key="i" v-if="analysis.id!=34">
                                        <div class="col-lg-1">{{i+1}}</div>
                                        <div class="col-lg-6">{{analysis.name}}</div>
                                        <div class="col-lg-1">
                                            <button class="btn" 
                                                :class="{'btn-success':analysis.state=='1','btn-default':analysis.state!='1'}" 
                                                @click.prevent="analysis.state=='1'?analysis.state='0':analysis.state='1'">
                                                是
                                            </button>
                                            <input type="hidden" :value="analysis.state" :name="'result['+analysis.id+'][state]'">
                                        </div>
                                        <div class="col-lg-2">
                                            <AnalyzeFileInput :files="analysis.file" :name="'result['+analysis.id+'][file][]'"></AnalyzeFileInput>
                                        </div>
                                        <div class="col-lg-2">
                                            <input type="text" class="form-control" :name="'result['+analysis.id+'][remark]'" v-model="analysis.remark">
                                        </div>
                                    </div>
                                    <div class="row" v-else :key="i">
                                        <div class="col-lg-1">{{i+1}}</div>
                                        <div class="col-lg-4">{{analysis.name}}</div>
                                        <div class="col-lg-1"><div v-if="analysis.fake_info.pn"><b>仿冒对象</b>：<br>{{analysis.fake_info.pn}}</div></div>
                                        <div class="col-lg-1"><ResizeImg v-if="analysis.fake_info.icon" :src="icon34_base_url+sample.sample_hash"></ResizeImg></div>
                                        <div class="col-lg-1">
                                            <button class="btn" 
                                                :class="{'btn-success':analysis.state=='1','btn-default':analysis.state!='1'}" 
                                                @click.prevent="analysis.state=='1'?analysis.state='0':analysis.state='1'">
                                                是
                                            </button>
                                            <input type="hidden" :value="analysis.state" :name="'result['+analysis.id+'][state]'">
                                        </div>
                                        <div class="col-lg-2">
                                            <AnalyzeFileInput :files="analysis.file" :name="'result['+analysis.id+'][file][]'"></AnalyzeFileInput>
                                        </div>
                                        <div class="col-lg-2">
                                            <input type="text" class="form-control" :name="'result['+analysis.id+'][remark]'" v-model="analysis.remark">
                                        </div>
                                    </div>
                                </template>
                                
                                
                            </template>
                        </div>
                        <div class="row">
                            <div class="col-lg-10">
                                <div class="form-group">
                                    <label>其他备注</label>
                                    <div class="input-group m-b">
                                        <div class="input-group-btn" :class="{'open':phrase_open}">
                                            <button data-toggle="dropdown" class="btn btn-white dropdown-toggle" @click.prevent.stop="phrase_open=!phrase_open">添加常用语 <span class="caret"></span>
                                            </button>
                                            <ul class="my-dropdown-menu">
                                                <template v-for="(strv,strk,stri) in phrase_list">
                                                    <li :key="strk">
                                                        <a :class="{'my-dropdown-menu-item-selected':phrase_selected==stri}" href="###" @click.prevent="setPhrase(strv)">{{strv}}</a>
                                                    </li>
                                                </template>
                                            </ul>
                                        </div>
                                        <input type="text" class="form-control" name="analyze_remark" v-model="analyze_remark" @keydown="remarkinpKD($event)" @input="remarkinpIP($event)">
                                    </div>
                                </div>
                            </div>
                            <div class="col-lg-2">
                                <div class="form-group">
                                    <label>备注截图</label>
                                    <br>
                                    <AnalyzeFileInput :files="analyze_remark_files" :name="'file[]'"></AnalyzeFileInput>
                                </div>
                            </div>
                        </div>
                        <div class="row" v-if="sample.state=='9'">
                            <div class="col-lg-10 text-warning">审核通过/不通过原因：{{sample.uncheck_reason}}</div>
                            <div class="col-lg-2">审核截图：
                                <img v-if="check_file.length>0" class="file-img" :src="getImgSrc(check_file[0])" @click.prevent="showImgList(check_file)">
                            </div>
                        </div>
                        </form>
                    </div>

                    <div class="modal-footer">
                        <button type="button" class="btn btn-success" @click="save" :disabled="saving">
                        <i class="fa fa-refresh fa-spin fa-fw" v-show="saving"></i>
                        保存草稿 (快捷键:alt+1)
                        </button>
                        <button type="button" class="btn btn-primary" @click="check" :disabled="approvaling">
                        <i class="fa fa-refresh fa-spin fa-fw" v-show="approvaling"></i>
                        提交审核 (快捷键:alt+2)
                        </button>
                        <button type="button" class="btn btn-default" @click="close">
                        关闭
                        </button>
                    </div>
                </div>
            </div>
            <Gallery :files="check_file_arr" :base-url="upload_base_url" :open="check_img_list_show" @close="closeGallery"></Gallery>
        </div>
    </transition>
</template>
<script>
import {tip,ajax} from '../tools/tools.js'
import AnalyzeFileInput from './AnalyzeFileInput.vue'
import ResizeImg from './ResizeImg.vue'
import Gallery from './Gallery.vue'
import {cfg} from '../config/config.js'

export default {
    name: 'AnalyzeModal',
    components:{AnalyzeFileInput,ResizeImg,Gallery},
    data() {
        return {
            title_str:'分析',
            analyze_remark:'',
            analyze_remark_files:[],
            check_file:[],
            check_file_arr:[],
            check_img_list_show:false,
            doubt:'0',
            res_list:[],
            icon34_base_url:'http://192.168.12.74:7100/icon?sample_hash=',
            upload_base_url:cfg.baseUrl+'/Uploads/',
            base_url:cfg.baseUrl,
            approvaling:false,
            saving:false,
            auto_str:'',

            phrase_open:false,
            phrase_selected:-1,
            phrase_list: {
                '1':'样本无检出',
                '2':'需要注册，部分测试',
                '3':'需要登录，部分测试',
                '4':'强制登录，部分测试',
                '5':'强制更新，部分测试',
                '6':'安装失败，无法完成测试',
                '7':'样本异常，部分测试'
            }
            
        }
    },
    props:{
        sample:{
            type:Object,
            default:{}
        },
        opened:{
            type:Boolean,
            default:false
        }
    },
    watch:{
        opened(v){
            var self = this;
            self.bindingKey(v);
        },
        sample:{
            deep:true,
            handler(v,ov){
                var self = this;

                if(v.sample_hash){
                    self.analyze_remark = v.analyze_remark?v.analyze_remark:'';
                    self.analyze_remark_files = v.file==''?[]:v.file;
                    self.check_file = v.check_file;
                    self.doubt = v.doubt;
                    self.resultView();
                } else {
                    self.analyze_remark = '';
                    self.analyze_remark_files = [];
                    self.check_file
                }
            }
        },
        
        auto_str(v,ov){
            var self = this;
        }
    },
    methods: {
        bindingKey(f){
            var self = this;

            if(f){
                document.body.addEventListener('keydown',self.keyEvent,false);
            } else {
                document.body.removeEventListener('keydown',self.keyEvent,false);
            }
        },
        keyEvent(e){
            var self = this;
            if(e.altKey&&e.keyCode==49){
                if (window.event) {
                    e.returnValue = false;
                    e.preventDefault();
                } else {
                    e.preventDefault();
                }
                if(self.sample.sample_hash){
                    self.save();
                }
            }

            if(e.altKey&&e.keyCode==50){
                if (window.event) {
                    e.returnValue = false;
                    e.preventDefault();
                } else {
                    e.preventDefault();
                }
                if(self.sample.sample_hash){
                    self.check();
                }
            }

            if(e.keyCode==27){
                if (window.event) {
                    e.returnValue = false;
                    e.preventDefault();
                } else {
                    e.preventDefault();
                }
                if(self.sample.sample_hash&&self.$root.GALLERY_OPENED==false){
                    self.close();
                }
            }
        },
        resultView(){
            var self = this;
            var par = {
                sample_hash:self.sample.sample_hash,
                test_type:'3',
                is_adv:'1'
            };
            ajax.sampleResultView({
                param:par,
                cb(data){
                    self.res_list = [];
                    self.res_list = data.data.map((v,i)=>{
                        if(v.id=='34'){
                            var fobj = {};
                            v.fake_info.map((f)=>{
                                if(f.fake_type=='program name'){
                                    fobj.pn = f.fake_object;
                                }
                                if(f.fake_type=='icon'){
                                    fobj.ic = f.fake_object;
                                }
                            });
                            v.fake_info = fobj;
                        }
                        return v;
                    })
                }
            })
        },
        setDoubt(){
            var self = this;
            if(self.doubt=='0'){
                self.doubt = '1';
            } else {
                self.doubt = '0';
            }
        },
        setPhrase(s,replace){
            var self = this;
            if(replace){
                self.analyze_remark = s;
                self.phrase_open = false;
                return;
            }

            if(self.analyze_remark.length==0){
                self.analyze_remark += s;
            } else {
                self.analyze_remark += '，'+s;
            }
            
        },
        remarkinpKD(e){
            var self = this;
            var key_arr = [49,50,51,52,53,54,55];
            var idx = key_arr.indexOf(e.keyCode);
            var str = '';
            if(e.keyCode==13&&self.phrase_selected!=-1){
                e.preventDefault();
                str = self.phrase_list[Object.keys(self.phrase_list)[self.phrase_selected]];
                self.setPhrase(str,true);
            }

            if(e.keyCode==38){
                e.preventDefault();
                if(self.phrase_selected==0)
                    self.phrase_selected=6;
                else
                   self.phrase_selected--; 
            }
            if(e.keyCode==40){
                e.preventDefault();
                if(self.phrase_selected==6)
                    self.phrase_selected=0;
                else
                   self.phrase_selected++; 
            }
            
        },
        remarkinpIP(e){
            var val = e.target.value
            console.log(val);
            var self = this;
            var k_idx = -1;
            for (var k in self.phrase_list) {
                if (self.phrase_list.hasOwnProperty(k)) {
                    var v = self.phrase_list[k];
                    if(v.indexOf(val)!=-1){
                        k_idx = Object.keys(self.phrase_list).indexOf(k);
                        break;
                    }
                    
                }
            }

            if(k_idx!=-1){
                self.phrase_selected = k_idx;
                self.phrase_open = true;
            } else {
                self.phrase_open = false;
            }
        },
        save(){
            var self = this;             
            var fd = new FormData(document.getElementById('fdForm'));
            self.saving = true;
            self.auto_str = '';
            ajax.saveDraft({
                param:fd,
                cb(data){
                    self.saving = false;
                    self.$emit('analyzesavedone');
                }
            });

        },
        check(){
            var self = this;
            self.auto_str = '';
            
            setTimeout(function() {
                var isPass = false;
                [].forEach.call(document.querySelectorAll('#fdForm [name*=state]'),function(v){
                    if(v.value=='1'){
                        isPass = true;
                    }
                });
                [].forEach.call(document.querySelectorAll('#fdForm [name*=remark]'),function(v){
                    if(v.value.length>0){
                        isPass = true;
                    }
                });
            
                if(isPass==false){
                    tip.warn('请填写检测项，或填写备注信息');
                    return;
                }

                if(confirm('是否提交分析结果？')){              
                    var fd = new FormData(document.getElementById('fdForm'));
                    // self.$emit('analyzedone');
                    self.approvaling = true;
                    ajax.analyze({
                        param:fd,
                        cb(data){
                            self.approvaling = false;
                            self.res_list = [];
                            self.$emit('analyzedone');
                        },
                        failCb(data){
                            tip.warn(data.errInfo);
                        }
                    });
                } else {
                    self.approvaling = false;
                }
            }, 200);
            
        },
        close(){
            var self = this; 
            self.res_list = [];
            self.$emit('analyzeclose');
        },
        showImgList(files){
            var self = this;
            self.check_file_arr = files;
            self.check_img_list_show = true;
        },
        closeGallery(){
            var self = this;
            self.check_file_arr = [];
            self.check_img_list_show = false;
        },
        getImgSrc(v){
            return this.upload_base_url + v;
        }
    },
    mounted() {
        var self = this;
        document.body.onclick = function(){
            self.phrase_open = false;
        };
        
        
    }
}
</script>
<style scoped>
.list-box{
    max-height: calc( 100vh - 360px );
    overflow-y: auto;
    overflow-x: hidden;
    padding-right: 10px;
    width:calc( 100% + 20px );
}

.list-box>.row{
    padding-top: 3px;
    border-bottom: 1px solid #eee;
    transition: all .1s ease;
}

.list-box>.row:hover{
    border-bottom-color: #1ab394;
}

.close {
    position: absolute;
    right: 30px;
    top: 30px;
}

.modal-mask {
    position: fixed;
    z-index: 10001;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    background-color: rgba(0, 0, 0, .5);
    transition: opacity .3s ease;
}

.file-img{
    max-height: 34px;
    max-width: 34px;
    cursor: pointer;
}

.modal-wrapper {
    position: absolute;
    width: 100%;
    height: 1px;
    left: 0;
    right:0;
    top: 60px;
}

.modal-container {
    width: calc( 100% - 380px );
    margin: 0px auto;
    padding: 20px 30px;
    background-color: #fff;
    border-radius: 2px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, .33);
    transition: all .3s ease;
    font-family: Helvetica, Arial, sans-serif;
    position: relative;
}

.modal-header h3 {
    margin-top: 0;
    color: #42b983;
}

.modal-body{
    padding: 5px 30px 5px 30px
}
.modal-footer{
    border-top: none;
}

.modal-default-button {
    float: right;
}

.modal-enter {
    opacity: 0;
}

.modal-leave-active {
    opacity: 0;
}

.modal-enter .modal-container,
.modal-leave-active .modal-container {
    -webkit-transform: scale(1.1);
    transform: scale(1.1);
}

.open > .my-dropdown-menu{
    display: block;
}

.my-dropdown-menu{
    border: medium none;
    border-radius: 3px;
    box-shadow: 0 0 3px rgba(86, 96, 117, 0.7);
    display: none;
    float: left;
    font-size: 12px;
    left: 0;
    list-style: none outside none;
    padding: 0;
    position: absolute;
    text-shadow: none;
    bottom: 34px;
    z-index: 1000;
    background-color: #fff;
}

.my-dropdown-menu.keyinter{
    display: block;
}

.my-dropdown-menu > li > a:hover, .my-dropdown-menu > li > a:focus {
    color: #262626;
    text-decoration: none;
    background-color: #f5f5f5;
}

.my-dropdown-menu > li > a.my-dropdown-menu-item-selected:hover,.my-dropdown-menu > li > a.my-dropdown-menu-item-selected{
    color: #262626;
    text-decoration: none;
    background-color: #a5a5a5;
}

.my-dropdown-menu > li > a {
    border-radius: 3px;
    color: inherit;
    line-height: 25px;
    margin: 4px;
    text-align: left;
    font-weight: normal;
}

.my-dropdown-menu > li > a {
    display: block;
    padding: 8px 20px;
    clear: both;
    font-weight: normal;
    line-height: 1.42857143;
    color: #333;
    white-space: nowrap;
}
</style>
